<template>
  <a-modal
    v-model="visible"
    title="编辑"
    @ok="onSubmit"
    @cancel="close"
  >
    <a-form-model
      v-if="visible"
      ref="nodeForm"
      :model="form"
      :rules="rules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
      <a-form-model-item ref="nodename" label="节点名称" prop="nodename">
        <a-input
          v-model="form.nodename"
          placeholder="输入节点名称"
        />
      </a-form-model-item>

      <a-form-model-item ref="nodeip" label="节点ip" prop="nodeip">
        <a-input
          v-model="form.nodeip"
          placeholder="输入节点ip"
        />
      </a-form-model-item>

      <a-form-model-item ref="nodeport" label="节点端口" prop="nodeport">
        <a-input
          v-model="form.nodeport"
          placeholder="输入节点port"
        />
      </a-form-model-item>

      <a-form-model-item ref="nettyport" label="心跳监听端口" prop="nettyport">
        <a-input
          v-model="form.nettyport"
          placeholder="心跳监听端口"
        />
      </a-form-model-item>

      <a-form-model-item ref="gnrlid" label="绑定机构" prop="institutionsOrganizationTb.id">
        <a-select v-model="form.institutionsOrganizationTb.id"  placeholder="选择机构">
          <a-select-option v-for="item in orgData" :key="item.id" :value="item.id">
            {{ item.orgname }}
          </a-select-option>
        </a-select>
      </a-form-model-item>

      <a-form-model-item ref="prefixCode" label="节点前缀">
        <a-input disabled placeholder="节点前缀"></a-input>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>
<script>
  import {saveNode} from '@/api/rootnode'
  import {getNotBindOrg} from '@/api/organization'

  export default {
    name: 'Edit',
    data: function () {
      return {
        labelCol: {span: 8},
        wrapperCol: {span: 10},
        visible: false,
        form: {
          id: '',
          institutionsOrganizationTb: {
            id: undefined
          }
        },
        rules: {
          nodename: [
            {required: true, message: '请输入节点名称', trigger: 'blur'}
          ],
          nodeip: [
            {required: true, message: '请输入节点ip', trigger: 'blur'}
          ],
          nodeport: [
            {required: true, message: '请输入节点端口', trigger: 'blur'}
          ],
          nettyport: [
            {required: true, message: '请输入心跳监听端口', trigger: 'blur'}
          ],
          'institutionsOrganizationTb.id': [
            {required: true, message: '请选择机构', trigger: 'blur'}
          ],
        },
        orgData: []
      }
    },
    methods: {
      initData(obj) {
        this.visible = true
        if (obj.id) {
          this.$nextTick(() => {
            this.form = obj
          })
        }
        this.getOrgData()
      },
      getOrgData() {
        const _this = this
        getNotBindOrg().then(res => {
          _this.orgData = res.data
        })
      },
      onSubmit() {
        this.$refs.nodeForm.validate(valid => {
          if (valid) {
            saveNode(this.form).then(res => {
              if (res.code === '0') {
                this.$message.success('保存成功')
              } else {
                this.$message.error('保存失败')
              }
              this.close()
            }).catch(err => {
              this.$message.error(err.response.data.message)
            }).finally(() => {

            });
          }
        })
      },
      close() {
        this.form = {
          id: '',
          institutionsOrganizationTb: {
            id: undefined
          }
        }
        this.visible = false
        this.$emit('ok')
      }
    }
  }
</script>
